/**
 * Copyright (c) 2014, Jason Gardner
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package freecake.tenthousand.states;

import freecake.tenthousand.TenThousand;
import freecake.tenthousand.TransitionCallback;
import freecake.tenthousand.util.ScoringUtilities;

public class RulesPageState implements TenThousandState {

    public final boolean handleStateTransition(final TenThousand.GameState newState,
                                                TransitionCallback transitionCallback) {
        if (newState == TenThousand.GameState.RulesPage1) {
            printRulesPage(1);
            return true;
        } else if (newState == TenThousand.GameState.RulesPage2) {
            printRulesPage(2);
            return true;
        } else if (newState == TenThousand.GameState.RulesPage3) {
            printRulesPage(3);
            return true;
        }
        return false;
    }

    public final TenThousand.GameState handleUserInput(final TenThousand.GameState currentState, final int charCode) {
        if (currentState == TenThousand.GameState.RulesPage1 && charCode == 32) {
            return TenThousand.GameState.RulesPage2;
        } else if (currentState == TenThousand.GameState.RulesPage2 && charCode == 32) {
            return TenThousand.GameState.RulesPage3;
        } else if (currentState == TenThousand.GameState.RulesPage3 && charCode == 32) {
            return TenThousand.GameState.TurnStart;
        }
        return null; //not handled

    }

    private final void printRulesPage(final int pageNum) {
        switch (pageNum) {
            case 1:
                System.out.println("The object of the game is to reach a score of 10,000 before your");
                System.out.println("opponent. On a player's turn, the dice are rolled and, should the");
                System.out.println("player have at least one scoring die, the player can 'hold' them and");
                System.out.println("roll the remaining dice. If a player reaches a minimum of " +
                                                            ScoringUtilities.MIN_HOLD_THRESHOLD + " points");
                System.out.println("during their turn, they can choose to 'stay' (and collect their");
                System.out.println("points) or they could press their luck and continue rolling.");
                System.out.println("\npress [space] to continue...");
                break;
            case 2:
                System.out.println("If all 5 dice are held, and the player is continuing their turn, the");
                System.out.println("accumlated points rollover, and all 5 dice are rolled again. If, at");
                System.out.println("any point during a turn, a roll results in no new scoring dice, the");
                System.out.println("turn is over and any points accumulated are lost. Before a player is");
                System.out.println("allowed to 'stay' at the minimum " +
                                        ScoringUtilities.MIN_HOLD_THRESHOLD + " points, they must first be 'on");
                System.out.println("the board'. This is accomplished by reaching an initial minimum of");
                System.out.println(ScoringUtilities.ON_THE_BOARD_THRESHOLD + " points.");
                System.out.println("\npress [space] to continue...");
                break;
            case 3:
                System.out.println("=======================  Scoring Combinations  =======================");
                System.out.println("  5's           = 50pts                                               ");
                System.out.println("  1's           = 100pts                                              ");
                System.out.println("  3 of a kind   = 100pts * the die value (3 1's are worth 1000pts)    ");
                System.out.println("  Straight      = 750pts                                              ");
                System.out.println("  5 of a kind   = 1000pts * the die value                             ");
                System.out.println("======================================================================");
                System.out.println("\npress [space] to continue...");
                break;
        }
    }

}